package com.guhappy.model.user;

// Generated Jul 19, 2011 6:12:32 PM by Hibernate Tools 3.4.0.CR1

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.guhappy.util.HibernateListener;

/**
 * Home object for domain model class WebUsers.
 * @see  bd
 * @author  B.D
 */
public class WebUserHome {

	private static final Logger log = Logger.getLogger(WebUserHome.class);

	/**
	 * @uml.property  name="sessionFactory"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="com.guhappy.model.user.WebUsers"
	 */
	private static final SessionFactory sessionFactory = getSessionFactory();

	/**
	 * @return
	 * @uml.property  name="sessionFactory"
	 */
	protected static SessionFactory getSessionFactory() {
		SessionFactory sessionFactory = 
	         (SessionFactory) ServletActionContext.getServletContext()
                    .getAttribute(HibernateListener.KEY_NAME);
		return sessionFactory;
	}

	public static void persist(WebUser transientInstance) {
		log.debug("persisting WebUsers instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public static void attachDirty(WebUser instance) {
		log.debug("attaching dirty WebUsers instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void attachClean(WebUser instance) {
		log.debug("attaching clean WebUsers instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static void delete(WebUser persistentInstance) {
		log.debug("deleting WebUsers instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public static WebUser merge(WebUser detachedInstance) {
		log.debug("merging WebUsers instance");
		try {
			WebUser result = (WebUser) sessionFactory.getCurrentSession()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public static WebUser findById(java.lang.String id) {
		log.debug("getting WebUsers instance with id: " + id);
		Session session=null;
		WebUser webUsers=null;
		try{
			session = sessionFactory.getCurrentSession();
			Query q = session.createQuery("from com.guhappy.model.user.WebUser wu where wu.id = :id");
			q.setString("id", id);
			webUsers=(WebUser) q.uniqueResult();
		}
		catch (Exception e){
			System.out.println(e);
			
		}
		return webUsers;
	}
	
	public static WebUser findByEmail(java.lang.String email) {
		log.debug("getting WebUsers instance with email: " + email);
		Session session=null;
		WebUser webUsers=null;
		try{
			session = sessionFactory.getCurrentSession();
			Query q = session.createQuery("from com.guhappy.model.user.WebUser wu where wu.email = :email");
			q.setString("email", email);
			webUsers=(WebUser) q.uniqueResult();
		}
		catch (Exception e){
			System.out.println(e);
			
		}
		return webUsers;
	}

	public static WebUser findUser(String email, String password) {
		WebUser user = null;
		
		if (email == null || password == null) {
			return user;
		}
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
			//get the userProfile from the database
			user = (WebUser) session.createCriteria(com.guhappy.model.user.WebUser.class).add(Restrictions.eq("email", email.trim())).add(Restrictions.eq("userHash", password.trim())).list().get(0);
		}
		catch (Exception e){
			System.out.println(e);
			
		}
		return user;
		}
	
	public static boolean isExistUser(String email){
		long result = (Long) sessionFactory.getCurrentSession().createCriteria(com.guhappy.model.user.WebUser.class).add(Restrictions.eq("email", email.trim())).add(Restrictions.eq("status", "1")).setProjection(Projections.rowCount()).list().get(0);
		if (result >0)
			return true;
		else 
			return false;
	}
}
